package com.rd.draw.drawer.type;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import androidx.annotation.NonNull;
import com.rd.animation.type.AnimationType;
import com.rd.draw.data.Indicator;
import com.rd.draw.data.Orientation;
import com.rd.utils.CoordinatesUtils;

public class BasicDrawer extends BaseDrawer {

    private Paint strokePaint;
    private RectF rect;

    public BasicDrawer(@NonNull Paint paint, @NonNull Indicator indicator) {
        super(paint, indicator);

        strokePaint = new Paint();
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setAntiAlias(true);
        strokePaint.setStrokeWidth(indicator.getStroke());
        rect = new RectF();
    }

    public void draw(
            @NonNull Canvas canvas,
            int position,
            boolean isSelectedItem,
            int coordinateX,
            int coordinateY) {

        float radius = indicator.getRadius();
        int strokePx = indicator.getStroke();
        float scaleFactor = indicator.getScaleFactor();
        float itemWidth = indicator.getItemHalfWidth();

        int selectedColor = indicator.getSelectedColor();
        int unselectedColor = indicator.getUnselectedColor();
        int selectedPosition = indicator.getSelectedPosition();
        AnimationType animationType = indicator.getAnimationType();
        int color = unselectedColor;
        if (position == selectedPosition) {
            color = selectedColor;
        }
        paint.setColor(color);
        if(animationType == AnimationType.SLIDE && position == selectedPosition){
            int coordinate = CoordinatesUtils.getXCoordinate(indicator, position);
            if (indicator.getOrientation() == Orientation.HORIZONTAL) {
                rect.left = coordinate - itemWidth;
                rect.top = coordinateY - radius;
                rect.right = coordinate + itemWidth;
                rect.bottom = coordinateY + radius;
                canvas.drawRoundRect(rect, radius, radius, paint);
            } else {
                rect.left = coordinateX - radius;
                rect.top = coordinate - itemWidth;
                rect.right = coordinateX + radius;
                rect.bottom = coordinate + itemWidth;
                canvas.drawRoundRect(rect, radius, radius, paint);
            }
        }else {

            if (animationType == AnimationType.SCALE && !isSelectedItem) {
                radius *= scaleFactor;

            } else if (animationType == AnimationType.SCALE_DOWN && isSelectedItem) {
                radius *= scaleFactor;
            }

            if (animationType == AnimationType.WORM) {
                if(position > selectedPosition){
                    coordinateX = (int) (coordinateX + radius * 2);
                }
            }

            Paint paint;
            if (animationType == AnimationType.FILL && position != selectedPosition) {
                paint = strokePaint;
                paint.setStrokeWidth(strokePx);
            } else {
                paint = this.paint;
            }
            canvas.drawCircle(coordinateX, coordinateY, radius, paint);
        }
    }
}
